package com.jacob.cloud.mall.cartorder.controller;

import com.jacob.cloud.mall.cartorder.feign.ProductFeignClient;
import com.jacob.cloud.mall.cartorder.feign.UserFeignClient;
import com.jacob.cloud.mall.cartorder.model.vo.CartVO;
import com.jacob.cloud.mall.cartorder.service.ICartService;
import com.jacob.cloud.mall.common.common.ApiRestResponse;
import com.jacob.cloud.mall.user.model.pojo.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 购物车模块
 */
@RestController
public class CartController {

    @Autowired
    private ICartService cartService;

    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 购物车列表
     */
    @ApiOperation("购物车列表")
    @GetMapping("/cart/list")
    public ApiRestResponse<List<CartVO>> listCart() {
        System.out.println("ssssssssssssssss");
        User user = userFeignClient.getUser();

        List<CartVO> cartVOS = cartService.listCart(userFeignClient.getUser().getId());
        return ApiRestResponse.success(cartVOS);
    }

    /**
     * 添加商品到购物车
     */
    @ApiOperation("添加商品到购物车")
    @PostMapping("/cart/add")
    public ApiRestResponse<List<CartVO>> addCart(Integer productId, Integer count) {
        List<CartVO> cartVOS = cartService.addCart(productId, count, userFeignClient.getUser().getId());
        return ApiRestResponse.success(cartVOS);
    }

    /**
     * 添加商品到购物车
     */
    @ApiOperation("修改商品数量")
    @PostMapping("/cart/update")
    public ApiRestResponse<List<CartVO>> updateCart(@RequestParam("productId") Integer productId,
                                                    @RequestParam("count") Integer count) {
        List<CartVO> cartVOS = cartService.updateCart(productId, userFeignClient.getUser().getId(), count);
        return ApiRestResponse.success(cartVOS);
    }

    /**
     * 删除购物车的某个商品
     */
    @ApiOperation("删除购物车的某个商品")
    @PostMapping("/cart/delete")
    public ApiRestResponse<List<CartVO>> deleteCart(Integer productId) {
        List<CartVO> cartVOS = cartService.deleteCart(productId, userFeignClient.getUser().getId());
        return ApiRestResponse.success(cartVOS);
    }

    /**
     * 选中/不选中购物车的某个商品
     */
    @ApiOperation("选中/不选中购物车的某个商品")
    @PostMapping("/cart/select")
    public ApiRestResponse<List<CartVO>> selectCart(Integer productId, Integer selected) {
        List<CartVO> cartVOS = cartService.selectOrNot(productId, userFeignClient.getUser().getId(), selected);
        return ApiRestResponse.success(cartVOS);
    }

    /**
     * 全选/全不选购物车
     */
    @ApiOperation("全选/全不选购物车")
    @GetMapping("/cart/selectAll")
    public ApiRestResponse<List<CartVO>> selectAllCart(Integer selected) {
        List<CartVO> cartVOS = cartService.selectedAllOrNot(userFeignClient.getUser().getId(), selected);
        return ApiRestResponse.success(cartVOS);
    }
}
